<?php
//var_dump($_POST);
$ClassName = $_POST['ClassName'] ?? '';

// 把学生信息存储到数据库
$dsn = "sqlsrv:Server=localhost;Database=Student";
$db = new PDO($dsn, "sa", "123456");

$sql = "  select top 1 * from Class where ClassName = '{$ClassName}';";
$statement = $db->query($sql);
$selectResult = $statement->fetch(PDO::FETCH_ASSOC);
var_dump($selectResult);
if($selectResult) {
    echo "班级已经存在。";
    exit();
}

$sql = "insert into Class (ClassName) values ('{$ClassName}');";
$result = $db->exec($sql);
if ($result) {
    echo '增加班级成功，前往<a href="class_list.php">列表页</a>';
} else {
    echo '添加失败';
}

?>

